package com.wei.czz.framework.annex.controller;

import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.annex.AnnexAddEnumDto;
import com.wei.czz.common.dto.annex.AnnexDto;
import com.wei.czz.common.dto.annex.AnnexIndexEnumDto;
import com.wei.czz.common.dto.annex.AnnexPreDto;
import com.wei.czz.common.jsr303.group.Save;
import com.wei.czz.common.jsr303.group.Update;
import com.wei.czz.common.property.BatchProperty;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.annex.AnnexFormVo;
import com.wei.czz.common.vo.annex.AnnexVo;
import com.wei.czz.framework.annex.manager.AnnexManager;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-01-18 18:08:09
 * className: AnnexController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/annex")
@AllArgsConstructor
public class AnnexController {

    private static final Logger log = LoggerFactory.getLogger(AnnexController.class);

    private final BatchProperty batchProperty;

    private final AnnexManager annexManager;

    /**
     * 保存附件动态配置接口
     * <p>
     * 此接口会校验是否重复提交
     * @param annexVo 参数对象
     * @return 结果
     */
    @Resubmit(message = "保存附件动态配置")
    @PutMapping("/save")
    public Result<Object> saveAnnex(@Validated(Save.class) @RequestBody AnnexVo annexVo) {
        log.info("开始保存附件动态配置业务。{}", annexVo);

        annexManager.saveAnnex(annexVo);

        log.info("保存附件动态配置业务结束");
        return Result.success();
    }

    /**
     * 复制附件动态配置接口
     * <p>
     * 此接口会校验是否重复提交
     * @param id 主键
     * @return 结果
     */
    @Resubmit(message = "复制附件动态配置")
    @PutMapping("/copy")
    public Result<Object> copyAnnex(@RequestParam("id") Long id) {
        log.info("开始复制附件动态配置业务。id={}", id);

        annexManager.copyAnnex(id);

        log.info("复制附件动态配置业务结束");
        return Result.success();
    }

    /**
     * 分页获取附件动态配置接口
     * @param annexFormVo 参数对象
     * @return 结果
     */
    @PostMapping("/page/list")
    public Result<PageDto<AnnexPreDto>> getAnnexPageList(@RequestBody AnnexFormVo annexFormVo) {
        log.info("开始分页查询附件动态配置业务。{}", annexFormVo);

        PageDto<AnnexPreDto> pageDto = annexManager.getAnnexPageList(annexFormVo);

        log.info("分页查询附件动态配置业务结束");
        return Result.success(pageDto);
    }

    /**
     * 获取附件动态配置接口
     * @param id 主键
     * @return 结果
     */
    @GetMapping("/get")
    public Result<AnnexDto> getAnnex(@RequestParam("id") Long id) {
        log.info("开始获取附件动态配置业务。id={}", id);

        AnnexDto annexDto = annexManager.getAnnex(id);

        log.info("获取附件动态配置业务结束");
        return Result.success(annexDto);
    }

    /**
     * 获取附件管理页面枚举接口
     * @return 结果
     */
    @GetMapping("/index/enums")
    public Result<AnnexIndexEnumDto> getAnnexIndexEnum() {
        log.info("开始获取附件管理页面枚举业务");

        AnnexIndexEnumDto annexIndexEnumDto = annexManager.getAnnexIndexEnum();

        log.info("获取附件管理页面枚举业务结束");
        return Result.success(annexIndexEnumDto);
    }

    /**
     * 获取附件添加页面枚举接口
     * @return 结果
     */
    @GetMapping("/add/enum")
    public Result<AnnexAddEnumDto> getAnnexAddEnum() {
        log.info("开始获取附件添加页面枚举业务");

        AnnexAddEnumDto annexAddEnumDto = annexManager.getAnnexAddEnum();

        log.info("获取附件添加页面枚举业务结束");
        return Result.success(annexAddEnumDto);
    }

    /**
     * 修改附件动态配置接口
     * <p>
     * 此接口会校验是否重复提交
     * @param annexVo 参数对象
     * @return 结果
     */
    @Resubmit(message = "修改附件动态配置")
    @PutMapping("/update")
    public Result<Object> updateAnnex(@Validated(Update.class) @RequestBody AnnexVo annexVo) {
        log.info("开始修改附件动态配置业务。{}", annexVo);

        annexManager.updateAnnex(annexVo);

        log.info("修改附件动态配置业务结束");
        return Result.success();
    }

    /**
     * 启用附件动态配置接口
     * <p>
     * 此接口会校验是否重复提交
     * @param id 附件动态配置前置主键
     * @return 结果
     */
    @Resubmit(message = "启用附件动态配置")
    @PutMapping("/use")
    public Result<Object> useAnnex(@RequestParam("id") Long id) {
        log.info("开始修改附件动态配置状态业务。id={}", id);

        annexManager.useAnnex(id);

        log.info("修改附件动态配置状态业务结束");
        return Result.success();
    }

    /**
     * 删除附件动态配置接口
     * <p>
     * 此接口会校验是否重复提交
     * @param idList 主键列表
     * @return 结果
     */
    @Resubmit(message = "删除附件动态配置")
    @DeleteMapping("/delete")
    public Result<Object> deleteAnnex(@RequestBody List<Long> idList) {
        log.info("开始删除附件动态配置业务。idList={}", idList);
        if (idList.isEmpty()) {
            return Result.clientFail("主键列表不能为空");
        }
        if (batchProperty.getMaxSize() < idList.size()) {
            log.warn("删除附件动态配置数量超限。batch.size={} size={}", batchProperty.getMaxSize(), idList.size());
            return Result.clientFail("一次删除最多允许同时删除" + batchProperty.getMaxSize() + "条数据");
        }

        annexManager.deleteAnnex(idList);

        log.info("删除附件动态配置业务结束");
        return Result.success();
    }

}
